﻿using System.Collections.Generic;


namespace HEFramework
{
    internal struct Msg
    {
        public bool error;
        public string content;

        public Msg(bool _error, string _content)
        {
            this.error = _error;
            this.content = _content;
        }
    }

    internal static class DataTableLog
    {
        private static bool errorFlag;
        private static List<Msg> msgList;

        public static bool ErrorFlag => errorFlag;

        public static string[] GetMsg(bool _error)
        {
            List<string> result = new List<string>();
            for (int i = 0; i < msgList.Count; i++)
            {
                if (_error == msgList[i].error)
                {
                    result.Add(msgList[i].content);
                }
            }

            return result.ToArray();
        }

        public static void Reset()
        {
            errorFlag = false;
            msgList = new List<Msg>();
        }

        public static void PrintLog(string _log)
        {
            msgList.Add(new Msg(false, _log));
        }

        public static void PrintLog(string _format, params object[] _args)
        {
            PrintLog(string.Format(_format, _args));
        }

        public static void PrintError(string _log)
        {
            errorFlag = true;
            msgList.Add(new Msg(true, _log));
        }

        public static void PrintError(string _format, params object[] _args)
        {
            PrintError(string.Format(_format, _args));
        }

        public static void PrintSheetError(string _file, string _sheet, string _errorMsg)
        {
            PrintError("{0}表格({1})--{2}.", _file, _sheet, _errorMsg);
        }

        public static void PrintCellError(string _file, string _sheet, int _row, int _column, string _errorMsg)
        {
            _row++;
            _column++;
            PrintError("{0}表格({1})--{2}行第{3}列({4},{5})--{6}", _file, _sheet, _row, _column, _row, ConvertToLetter(_column), _errorMsg);
        }

        private static string ConvertToLetter(int _i)
        {
            int a, b;
            string letter = "";
            while (_i > 0)
            {
                a = (int)((_i - 1) / 26);
                b = (_i - 1) % 26;
                letter = ((char)(b + 65)).ToString() + letter;
                _i = a;
            }

            return letter;
        }
    }
}